package com.jyserver.common.utils;


import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description :文件操作类
 */
public class JyFile {
    /**
     * @Description :获取文件内容
     */
    public static String FileInputStream(String path) throws IOException {
        File file = new File(path);
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行
                result.append(System.lineSeparator() + s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    /**
     * @Description :复制文件
     */
    public static boolean copyFile(String path, String newPath) {
        try {
            FileOutputStream fileOut = new FileOutputStream(newPath);
            InputStream input = new FileInputStream(path);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                fileOut.write(buf, 0, bytesRead);
            }
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * @Description :判断文件是否存在,不存在创建
     */
    public static File judeFileExists(File file) {
        if (file.exists()) {

        } else {
            try {
                String s = file.getAbsolutePath();
                s = s.replace("\\", "\\\\");
                String[] paths = s.split("\\\\");
                String path = paths[0];
                for (int i = 1; i < paths.length - 2; i++) {
                    path = path + "/" + paths[i];
                    judeDirExists(path);
                }
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return file;
    }

    /**
     * @Description :判断文件是否存在,不存在创建
     */
    public static File judeFileExists(String filepath) {
        return judeFileExists(new File(filepath));
    }

    /**
     * @Description :判断文件夹是否存在,不存在创建
     */
    public static File judeDirExists(File file) {

        if (file.exists()) {
            if (file.isDirectory()) {

            }
        } else {
            file.mkdir();
        }
        return file;
    }

    public static File judeDirExists(String filepath) {
        return judeDirExists(new File(filepath));
    }

    /**
     * @Description :文件写入
     */
    public static Boolean WriteToFile(String filepath, String content) {
        try {
            File file = judeFileExists(filepath);
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * @Description :文件写入
     */
    public static void WriteToFile(File file, String content) {
        try {
            file = judeFileExists(file);
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description :文件追加
     */
    public static void appendMethod(String fileName, String content) {
        appendMethod(judeFileExists(fileName), content);
    }

    /**
     * @Description :文件追加
     */
    public static void appendMethod(File file, String content) {
        FileWriter fw = null;
        try {
            File f = judeFileExists(file);
            fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println(content);
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description :获得文件夹下的所有文件名
     */
    public static List getFilesName(String strPath, String suffix) {
        List<String> filelist = new ArrayList<String>();
        File dir = new File(strPath);
        File[] files = dir.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                if (fileName.endsWith(suffix) || fileName.endsWith(suffix)) {
                    filelist.add(fileName);
                }
            }
        }
        return filelist;
    }

    /**
     * @Description :获得文件夹下的所有文件名(包括子集)
     */
    public static List recursiveFiles(String path, String suffix) {
        List filesList = new ArrayList<File>();
        // 创建 File对象
        File file = new File(path);

        // 取 文件/文件夹
        File files[] = file.listFiles();


        // 目录下文件
        if (files == null || files.length == 0) {
//            System.err.println(path + "该文件夹下没有文件");
            return filesList;
        }

        // 存在文件 遍历 判断
        for (File f : files) {

            // 判断是否为 文件夹
            if (f.isDirectory()) {

                // 为 文件夹继续遍历
                List filesListSon = recursiveFiles(f.getAbsolutePath(), suffix);
                for (int i = 0; i < filesListSon.size(); i++) {
                    filesList.add(filesListSon.get(i));
                }
                // 判断是否为 文件
            } else if (f.isFile()) {
                if (f.getName().endsWith(suffix)) {
                    filesList.add(f);
                }
            } else {
//                System.out.print("未知错误文件");
            }

        }
        return filesList;
    }
}
